import axios from 'axios'
const setAxios = axios.create({
  timeout: 10000
})

// 请求前拦截
setAxios.interceptors.request.use(
  function (config) {
    config.withCredentials = true // 允许携带token ,这个是解决跨域产生的相关问题
    axios.defaults.headers.common['Content-Type'] = 'application/json;charset=UTF-8'
    config.headers = {'Access-token': 123}
    return config
  },
  function (err) {
    return Promise.reject(err)
  }
)

// 请求后拦截
setAxios.interceptors.response.use(
  function (response) {
    return response
  },
  function (err) {
    return Promise.reject(err)
  }
)

// 请求封装 get post put del
const http = {
  get (url, params) {
    return setAxios.get(url, { params })
  },
  post (url, data) {
    return setAxios.post(url, data)
  },
  put (url, data) {
    return setAxios.put(url, data)
  },
  del (url) {
    return setAxios.delete(url)
  }
}

export default http
